﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace testSerrialeForm
{
    
    public partial class Form1 : Form
    {
        private const string PORT = "com";
        private string buffer = "";
        private string tmp = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                
                this.buffer = serialPort1.ReadExisting();

            }
            catch (IOException e4)
            {
                MessageBox.Show("Errore IO :" + e4.Message);
            }
            catch (InvalidOperationException e4)
            {
                MessageBox.Show("Errore Invalid :" + e4.Message);
            }
            catch (TimeoutException e4)
            {
                MessageBox.Show("Errore Invalid :" + e4.Message);
            }
        }

        private void com_Click(object sender, EventArgs e)
        {
            //Console.WriteLine("scrivo {0}",sender.ToString());
            try
            {
                string send = sender.ToString();
                if (send.IndexOf(PORT) >= 0)
                {

                    if (!this.serialPort1.IsOpen)
                    {
                        this.serialPort1.PortName = send;
                        this.serialPort1.Open();
                    }
                    else
                    {
                        this.serialPort1.Close();
                        this.serialPort1.PortName = send;
                        this.serialPort1.Open();
                    }
                }
                else
                {
                    if (this.serialPort1.IsOpen) this.serialPort1.Close();

                }
            }
            catch (IOException e1)
            {
                MessageBox.Show("Errore nell'apertura della porta\n" + e1.Message);
            }
            catch (InvalidOperationException e1)
            {
                MessageBox.Show( e1.Message);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.serialPort1.IsOpen) this.serialPort1.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            this.textBox1.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.serialPort1.WriteLine(textBox1.Text+"\r\n");
                this.textBox1.Clear();
            }catch(InvalidOperationException e3)
            {
                MessageBox.Show("Aprile la porta seriale!!!! \n" + e3.Message);

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.buffer.Length > 0)
            {
                if (buffer.IndexOf('\r') >= 0)
                {
                    this.tmp += this.buffer;
                    this.listBox1.Items.Add(this.tmp);
                    this.listBox1.Update();
                    this.tmp = "";
                }
                else
                {
                    this.tmp += this.buffer;
                }
                
                this.buffer = "";
            }

        }
    }
}
